package luren.king.wechatlite.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import luren.king.wechatlite.pojo.ChatMessage;

public class MessageDataBaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "chat.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "messages";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_MESSAGE_TEXT = "message_text";
    private static final String COLUMN_MESSAGE_SENDER = "message_sender";
    private static final String COLUMN_MESSAGE_TYPE = "message_type";


    public MessageDataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

    }

    public MessageDataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    public MessageDataBaseHelper(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, name, null, version);
    }

    public MessageDataBaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建表的SQL语句
        String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" +
                // 列：ID，整型，主键，自增
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                // 列：消息文本，文本类型
                COLUMN_MESSAGE_TEXT + " TEXT, " +
                // 这里是发送的对象
                COLUMN_MESSAGE_SENDER + " VARCHAR(25), " +
                // 列：消息类型，整型
                COLUMN_MESSAGE_TYPE + " INTEGER)";
        // 执行SQL语句创建表
        db.execSQL(createTableQuery);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 如果存在旧版本的表，则删除该表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        // 调用onCreate方法重新创建表
        onCreate(db);
    }


    public void insertMessage(String Sender, String messageText, int messageType) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_MESSAGE_SENDER, Sender);
        values.put(COLUMN_MESSAGE_TEXT, messageText);
        values.put(COLUMN_MESSAGE_TYPE, messageType);
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public List<ChatMessage> getAllMessages(String Email) {
        List<ChatMessage> messages = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[]{COLUMN_MESSAGE_TEXT, COLUMN_MESSAGE_TYPE}, COLUMN_MESSAGE_SENDER + " = ?", new String[]{Email}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String messageText = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE_TEXT));
                int messageType = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE_TYPE));
                ChatMessage message = new ChatMessage(messageText, messageType);
                messages.add(message);
            }
            cursor.close();
        }
        db.close();
        return messages;
    }
}
